package com.inspur.dgov.policy.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inspur.dgov.common.tool.api.R;
import com.inspur.dgov.common.tool.constant.AppConstant;
import com.inspur.dgov.policy.entity.FileAnalysis;
import com.inspur.dgov.policy.entity.FilePolicy;
import com.inspur.dgov.policy.entity.InfoPolicy;
import com.inspur.dgov.policy.service.EsPolicyService;
import com.inspur.dgov.policy.service.FileAnalysisService;
import com.inspur.dgov.policy.service.FilePolicyService;
import com.inspur.dgov.policy.service.InfoPolicyService;
import com.inspur.dgov.policy.vo.FileAnalysisVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * <p>
 * 政策解读信息表 前端控制器
 * </p>
 *
 * @author chiyupei
 * @since 2020-04-10
 */
@RestController
@RequestMapping("/analysis")
@Api(value = "政策解读信息", tags = "政策解读信息")
@Slf4j
public class FileAnalysisController {

    @Autowired
    private FileAnalysisService fileAnalysisService;

    @Autowired
    private FilePolicyService filePolicyService;

    @Autowired
    private InfoPolicyService infoPolicyService;

    @Autowired
    private EsPolicyService esPolicyService;

    @Value("${es.push.flag}")
    private boolean esPushFlag;

    /**
     * 政策解读信息列表
     */
    @PostMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basicTitle", value = "标题", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "startDate", value = "解读日期（起）", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "endDate", value = "解读日期（止）", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "basicStatus", value = "状态", paramType = "query", dataType = "string")
    })
    @ApiOperation(value = "政策解读信息列表", notes = "传入标题、解读日期范围或状态", position = 1)
    public R<IPage<FileAnalysis>> page(@ApiIgnore @RequestParam Map<String, Object> query, Page page) {
        IPage<FileAnalysis> pages = this.fileAnalysisService.selectAnalysisPage(page, query);
        return R.data(pages);
    }

    /**
     * 撤回政策解读
     */
    @PostMapping("/back")
    @ApiOperation(value = "撤回政策解读", notes = "传入政策解读ID", position = 2)
    public R back(@RequestBody FileAnalysis analysis) {
        if (esPushFlag) {
            //从ES删除该政策
            this.esPolicyService.deleteEsPolicy(analysis.getId());
        }
        //更新发布状态
        FileAnalysis param = new FileAnalysis();
        param.setId(analysis.getId());
        param.setBasicState(AppConstant.DB_STATUS_ABNORMAL);
        boolean ret = this.fileAnalysisService.updateById(param);
        return R.status(ret);
    }

    /**
     * 删除政策解读
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除政策解读", notes = "传入政策解读ID", position = 3)
    public R remove(@RequestBody FileAnalysis analysis) {
        //判断是否是已撤回状态
        FileAnalysis record = this.fileAnalysisService.getById(analysis.getId());
        if (!AppConstant.DB_STATUS_ABNORMAL.equals(record.getBasicState())) {
            return R.fail("该政策解读已经发布，不能删除");
        }
        //物理删除
        boolean ret = this.fileAnalysisService.removeById(analysis.getId());
        return R.status(ret);
    }

    /**
     * 政策解读信息详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "政策解读信息详情", notes = "政策解读信息详情数据", position = 4)
    public R<FileAnalysisVO> detail(@RequestBody FileAnalysis analysis) {
        FileAnalysisVO analysisPolicyVo = new FileAnalysisVO();

        //获取政策解读详情
        FileAnalysis detail = this.fileAnalysisService.getById(analysis.getId());
        BeanUtil.copyProperties(detail, analysisPolicyVo);

        //获取政策解读文件内容
        if (StrUtil.isNotBlank(detail.getBasicContentId())) {
            FilePolicy filePolicy = this.filePolicyService.getById(detail.getBasicContentId());
            if (null != filePolicy) {
                if (StrUtil.isBlank(filePolicy.getContentHtml())) {
                    analysisPolicyVo.setContentHtml(filePolicy.getContent());
                } else {
                    analysisPolicyVo.setContentHtml(filePolicy.getContentHtml());
                }
            }
        }

        //获取关联政策
        String policyId = detail.getPolicyId();
        if (StrUtil.isNotBlank(policyId)) {
            InfoPolicy policy = this.infoPolicyService.getById(policyId);
            analysisPolicyVo.setPolicyName(policy.getBasicTitle());
        }
        return R.data(analysisPolicyVo);
    }
}
